
package model.graphic.objects;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.util.Vector;

/**
 *
 * @author Ben
 */
public class GSine extends Graphical{
    private double x1,x2, y1,y2;

    public GSine(double x1, double y1, double x2, double y2) {
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    public GSine(){
        this(0,0,0,0);
    }

    public double getX1() {
        return x1;
    }

    public double getX2() {
        return x2;
    }

    public double getY1() {
        return y1;
    }

    public double getY2() {
        return y2;
    }

    public void	setLine(double x1, double y1, double x2, double y2){
        this.x1 = x1;
        this.x2 = x2;
        this.y1 = y1;
        this.y2 = y2;
    }

    private Vector<Point> createSine(){
        Vector<Point> points = new Vector<Point>();
        if(x1 < x2){//draw an ordinary sine
            double prevX = x1, prevY =y1;
            double yAxis = (y1+y2)/2;
            double amplitude = (y1 - y2)/2;
            //y point at x1 is sin(90°), y point at x2 is sin(270°)
            double xSpace =  180 / Math.abs(x2 - x1);//270° - 90° = 180°
            points.add(new Point((int)prevX, (int)prevY));
            for(double x = x1; x < x2; x+= 10){
                double radians = Math.toRadians(90 + (x-x1)*xSpace);
                double y = Math.sin(radians) * amplitude + yAxis;
                points.add(new Point((int)x, (int)y));
                prevX = x;
                prevY = y;
            }
            points.add(new Point((int)x2, (int)y2));
        }else{//draw a sine that's rotates 90° by switching the axis
            double xAxis = (x1+x2)/2;
            double amplitude = (x1 - x2)/2;
            if(y1 > y2){
                amplitude *= -1;
            }
            int yStart = (int)Math.min(y1,y2);
            int yEnd = (int)Math.max(y1,y2);
            double prevY =yStart;
            double prevX = prevY == y1? x1 : x2;
            double xEnd = yEnd == y1? x1 : x2;
            double xSpace =  180 / Math.abs(y2 - y1);
            points.add(new Point((int)prevX, (int)prevY));
            for(int x = yStart; x < yEnd; x+= 10){
                double radians = Math.toRadians(90 + (x-yStart)*xSpace);
                double y = (int)(Math.sin(radians) * amplitude) + xAxis;
                points.add(new Point((int)y, (int)x));
                prevX = y;
                prevY = x;
            }
            points.add(new Point((int)xEnd, (int)yEnd));
            if(yStart==yEnd){//limit case:  horizontal positions
                points.add(new Point((int)x1, (int)y1));
                points.add(new Point((int)x2, (int)y2));
            }
       }
        return points;
    }

    public Polygon createPolygon() {
        Polygon polygon = new Polygon();
        Vector<Point> points = createSine();
        for(int i = 0; i < points.size() ; i++){
            Point p = points.get(i);
            polygon.addPoint((int) p.getX(), (int) p.getY() + 8);
        }
        for(int i = points.size()-1; i >= 0 ; i--){
            Point p = points.get(i);
            polygon.addPoint((int) p.getX(), (int) p.getY() - 8);
        }
        return polygon;
    }

    @Override
    public void paint(Graphics2D g) {
        Vector<Point> points = createSine();
        for(int i = 1; i < points.size() ; i++){
            Point prev = points.get(i-1);
            Point cur = points.get(i);
            g.drawLine((int)prev.getX(), (int)prev.getY(), (int)cur.getX(), (int)cur.getY());
        }
    }

}
